package com.qisu.command;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.qisu.cacheMap.OrderIdCache;
import com.qisu.converters.Converters;
import com.qisu.service.OrderService;

import io.netty.channel.ChannelHandlerContext;

/**
 * Acknowledgment of start charge from station
 *
 * @author alsan
 *
 */
@Component
public class CMD8 extends AbstractSinexcelCmd {
	@Autowired private OrderService orderService;
	@Autowired private OrderIdCache orderIdCache;
//	@Autowired private Jsonify _json;

	@PostConstruct
	@Override
	protected void init() {
		super.init();

		fieldsList.addField(new CommandFieldDef<Integer>(2))
				.addField(new CommandFieldDef<Integer>(2))
				.addField(new CommandFieldDef<String>(32, Converters.strConverter, "充电桩ID"))
				.addField(new CommandFieldDef<Byte>(1, Converters.byteConverter, "枪口号"))
				.addField(new CommandFieldDef<Integer>(2, Converters.intConverter, "执行结果"));
	}

	@Override
	protected void processCmd(ChannelHandlerContext ctx, byte[] req) {
		int result = fieldsList.getValue("执行结果");
		String pileSerial = fieldsList.getValue("充电桩ID");
		int gunNo = ((Byte)fieldsList.getValue("枪口号")).intValue();
		/*HashMap<String, Object> packetData = new HashMap<String, Object>() {
			private static final long serialVersionUID = 6417934495723576777L;

			{
				put("充电桩ID", _fieldsList.getValue("充电桩ID"));
				put("枪号", _fieldsList.getValue("枪口号"));
				put("执行结果", _fieldsList.getValue("执行结果"));
			}
		};

		_publisher.publish("notify/ack/start-charge", pileSerial);
		_publisher.publish("data/ack/start-charge/" + pileSerial, _json.writeValueAsString(packetData));
*/
		if(0 == result) {
			String orderId = orderIdCache.get(pileSerial+gunNo);
			orderService.startCharge(orderId);
		} else {
		}
	}

	@Override
	protected void responseClient(ChannelHandlerContext ctx) {
		// TODO Auto-generated method stub

	}

}
